package cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.service;

import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.Instruction;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.mapper.InstructionMapper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.util.SnowflakeIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 张烁瑜
 * @Date 2025/5/26 20:40
 */
@Service
public class InstructionService {

    @Autowired
    private InstructionMapper instructionMapper;
    @Autowired
    private SnowflakeIdGenerator idGenerator; // 注入雪花ID生成器

    public String save(Instruction instruction) {
        // 生成雪花ID并设置到实体
        long snowflakeId = idGenerator.generateId();
        instruction.setId(snowflakeId);

        if (instructionMapper.save(instruction)) {
            return "保存成功";
        }
        return "保存失败";
    }

    public String delete(Long id) {
        if (instructionMapper.delete(id)) {
            return "删除成功";
        }
        return "删除失败";
    }

    public String update(Instruction instruction) {
        if (instructionMapper.update(instruction)) {
            return "更新成功";
        }
        return "更新失败";
    }

    public PageInfo<Instruction> get(int pageNum, int pageSize, Instruction instruction) {
        PageHelper.startPage(pageNum, pageSize);
        List<Instruction> instructionList = instructionMapper.get(instruction);
        return PageInfo.of(instructionList);
    }
}